package com.ruoyi.business.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.domain.DeliveryBillentryHand;
import com.ruoyi.business.service.DeliveryBillentryHandService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 发货物料明细校核Controller
 *
 * @author JerryLu
 * @date 2024/9/7 14:23
 */
@RestController
@RequestMapping("/system/delivery_billentry_hand")
public class DeliveryBillentryHandController extends BaseController {
    @Autowired
    private DeliveryBillentryHandService billentryHandService;

    /**
     * 查询发货物料明细校核列表
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeliveryBillentryHand deliveryBillentryHand) {
        startPage();
        List<DeliveryBillentryHand> list = billentryHandService.queryList(deliveryBillentryHand);
        return getDataTable(list);
    }

    /**
     * 导出发货物料明细校核列表
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:export')")
    @Log(title = "发货物料明细校核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliveryBillentryHand deliveryBillentryHand) {
        List<DeliveryBillentryHand> list = billentryHandService.queryList(deliveryBillentryHand);
        ExcelUtil<DeliveryBillentryHand> util = new ExcelUtil<DeliveryBillentryHand>(DeliveryBillentryHand.class);
        util.exportExcel(response, list, "发货物料明细校核数据");
    }

    /**
     * 获取发货物料明细校核详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(billentryHandService.getById(id));
    }

    /**
     * 获取发货物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:delivery:index')")
    @GetMapping(value = "/receiving/{deliveryId}")
    public AjaxResult getReceiptInfo(@PathVariable("deliveryId") String deliveryId) {
        QueryWrapper<DeliveryBillentryHand> query = Wrappers.query();
        query.eq("delivery_id", deliveryId);
        List<DeliveryBillentryHand> list = billentryHandService.list(query);
        return success(list);
    }

    /**
     * 新增发货物料明细校核
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:add')")
    @Log(title = "发货物料明细校核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryBillentryHand deliveryBillentryHand) {
        return toAjax(billentryHandService.save(deliveryBillentryHand));
    }

    /**
     * 修改发货物料明细校核
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:edit')")
    @Log(title = "发货物料明细校核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JSONObject json) {
        try {
            Long id = json.getLong("id");
            if (json.getString("form") != null) {
                List<DeliveryBillentryHand> arr = JSON.parseArray(json.getString("form"), DeliveryBillentryHand.class);
                for (DeliveryBillentryHand hand : arr) {
                    hand.setDeliveryId(id);
                    hand.setUpdateTime(DateUtils.getNowDate());
                    billentryHandService.updateById(hand);
                }
            }
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除发货物料明细校核
     */
    @PreAuthorize("@ss.hasPermi('system:delivery_billentry_hand:remove')")
    @Log(title = "发货物料明细校核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(billentryHandService.removeBatchByIds(Arrays.asList(ids)));
    }
}

